using System;
using System.Linq;
using CloudStorage.API.Exceptions;
using CloudStorage.Properties;

namespace CloudStorage.API.Messages.Extensions
{
	/// <summary>
	/// Extensions on <see cref="IQueueStorageAccount"/>
	/// </summary>
	public static class IQueueStorageAccountExtensions
	{
		/// <summary>
		/// Checks to see if a named queue exists.
		/// </summary>
		/// <param name="queueStorageAccount">Account to check for the existance of the queue.</param>
		/// <param name="queueName">Name of the queue to find.</param>
		/// <returns>True if the given queue exists</returns>
		/// <exception cref="ArgumentNullException">Expects a non-null, non-empty queueName</exception>
		/// <exception cref="CloudStorageException">If the result returned contains more than one queue that matches the prefix that is the queueName.</exception>
		public static bool QueueExists(this IQueueStorageAccount queueStorageAccount, string queueName)
		{
			if (string.IsNullOrEmpty(queueName))
			{
				throw new ArgumentNullException("queueName", Strings.MissingQueueName);
			}

			return queueStorageAccount.ListQueues(queueName)
																.Items
																.Where(lqi => lqi.Name == queueName)
																.Count()
																.Equals(1);
		}
	}
}